/*
 * anima/x86_64/entry.S - Entry points to x86-64 versions of the microkernel.
 * Copyright (C) 2007  Mikhail Vorozhtsov
 * See the LICENSE section of the README file for details.
 */

/* $Id$ */

.global elbp_entry_point
.global ap_entry_point

#define STACK_SIZE 4096

elbp_entry_point:
  /* Set up temprorary stack. */
  mov $(stack + STACK_SIZE), %rsp
  /* Pass multiboot magic and info as arguments. */
  mov %rax, %rdi
  mov %rbx, %rsi
  /* Call C code. */
  call init_bsp
  /* Halt on failure. */
  hlt

/* Application Processor entry point. */
.align 8
ap_entry_point:
  /* We are in real mode. */
  .code16
  /* Disable interrupts. */
  cli
  /* Load data segment. */
  mov %cs, %ax
  mov %ax, %ds
  /* Set up temprorary GDT. */
  lgdt ap_gdt_desc - ap_entry_point
  /* Switch to protected mode. */
  xor %ax, %ax
  inc %ax
  lmsw %ax
  /* Far jump to 32-bit code. */
  ljmpl $0x08, $1f
1:
  /* We are in 32-bit protected mode. */
  .code32
  /* Load data segments. */
  mov $0x10, %eax
  mov %eax, %ss
  mov %eax, %ds
  mov %eax, %es
  mov %eax, %fs
  mov %eax, %gs
  /* Set up temprorary stack. */
  mov $(stack + STACK_SIZE), %esp
  /* Call C code. */
  push $init_ap
  call init32_ap
  /* Halt on failure. */
  hlt

.align 16
ap_gdt_desc:
  .word 24
  .long ap_gdt

.align 16
ap_gdt:
  /* First item - invalid. */
  .long 0x00000000
  .long 0x00000000
  /* Second item - code. */
  .long 0x0000FFFF
  .long 0x00CF9A00
  /* Third item - data. */
  .long 0x0000FFFF
  .long 0x00CF9200

.section .bss

.align 8
stack:
  .space STACK_SIZE

